package com.emma.controller;

import com.emma.entity.ResultInfo;
import com.emma.entity.User;
import com.emma.service.UserServie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserServie userServie;
    @Autowired
    private HttpSession session;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;


    @RequestMapping("/login")
    public ResultInfo login(@RequestBody Map<String, Object> paramsMap){

        User user = userServie.findUser(paramsMap);
        if (user==null){
            System.out.println("用户名或密码错误");
            return new ResultInfo(false,"用户名或密码错误");

        }else{
            System.out.println("登录成功");
            session.setAttribute("user",user);
            return new ResultInfo(true,"登录成功");
        }

    }

    @GetMapping("/logout")
    public void logout(){

        session.invalidate();


    }


}
